# Contributor: Borys Zhukov <mp5@mp5.im>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: omni <omni+alpine@hack.org>
pkgname=ocaml
pkgver=4.14.2
pkgrel=1
pkgdesc="Main implementation of the Caml programming language"
url="https://ocaml.org/"
# riscv64: hangs forever at final build step
# loongarch64: no upstream support
arch="all !riscv64 !loongarch64"
license="LGPL-2.1-or-later WITH OCaml-LGPL-linking-exception"
depends="$pkgname-runtime=$pkgver-r$pkgrel gcc"
makedepends="gdbm-dev libc-dev ncurses-dev zlib-dev binutils-dev rpm-scripts"
checkdepends="parallel"
subpackages="
	$pkgname-abuild-helper:_abuild_helper:noarch
	$pkgname-doc
	$pkgname-byte
	$pkgname-ocamldoc
	$pkgname-compiler-libs:_compiler_libs
	$pkgname-runtime
	"
source="https://caml.inria.fr/pub/distrib/ocaml-${pkgver%.*}/ocaml-$pkgver.tar.gz
	ocaml4-abuild-find
	"

# 32bit archs
case "$CARCH" in
armv7|armhf|x86)
	options="$options textrels"
	export LDFLAGS="$LDFLAGS -Wl,-z,notext"
	;;
esac

build() {
	local extra_conf_opts
	if ! subpackage_types_has byte; then
		extra_conf_opts="
			--disable-installing-bytecode-programs
			"
	fi

	./configure \
		--prefix /usr \
		--bindir /usr/bin \
		--libdir /usr/lib/ocaml \
		--mandir /usr/share/man \
		$extra_conf_opts \
		CC="${CC:-gcc}" \
		AS="${CC:-gcc} -c" \
		ASPP="${CC:-gcc} -c"
	make world.opt
}

check() {
	make ocamltest
	make -C testsuite parallel
}

package() {
	make DESTDIR="$pkgdir" install

	install -Dm644 LICENSE "$pkgdir"/usr/share/licenses/$pkgname/LICENSE
	install -Dm644 Changes "$pkgdir"/usr/share/doc/$pkgname/Changes

	cd "$pkgdir"

	# Remove annotation files and sources.
	find usr/lib/ocaml \
		\( -name '*.cmt' -o -name '*.cmti' -o -name '*.ml' \) \
		-a -delete

	# To be consistent with other binaries.
	if subpackage_types_has byte; then
		mv usr/bin/ocamldoc usr/bin/ocamldoc.byte
	else
		rm usr/bin/ocamldoc
	fi
	ln -s ocamldoc.opt usr/bin/ocamldoc
}

byte() {
	pkgdesc="$pkgdesc (bytecode executables)"
	replaces="$pkgname-dev"

	amove usr/bin/*.byte
}

ocamldoc() {
	pkgdesc="$pkgdesc (documentation generator)"
	depends="$pkgname=$pkgver-r$pkgrel"
	replaces="$pkgname"  # for backward compatibility

	amove usr/bin/ocamldoc*
	amove usr/lib/ocaml/ocamldoc
}

_compiler_libs() {
	pkgdesc="$pkgdesc (compiler libraries)"
	depends="$pkgname=$pkgver-r$pkgrel"
	replaces="$pkgname"  # for backward compatibility

	amove usr/lib/ocaml/compiler-libs
}

runtime() {
	pkgdesc="$pkgdesc (runtime environment)"
	depends=""
	replaces="$pkgname"  # for backward compatibility

	amove usr/bin/ocamlrun*
	amove usr/lib/ocaml/ld.conf

	local i; for i in \
		'*.cmo' \
		'*.cmi' \
		'*.cma' \
		'stublibs' \
		'threads/*.cmi' \
		'threads/*.cma'
	do
		amove "usr/lib/ocaml/$i"
	done
}

_abuild_helper() {
	pkgdesc="Helper script for OCaml 4 dependency tracing"
	depends="$pkgname=$pkgver-r$pkgrel rpm-scripts"
	install_if="$pkgname=$pkgver-r$pkgrel build-base"

	install -Dvm755 ocaml4-abuild-find -t "$subpkgdir"/usr/bin/
}

prepare_py_provides() {
	local dir="${subpkgdir:-$pkgdir}" ocaml_providers=""
	local pkgbasedir=${pkgbasedir:-"$startdir/pkg"}
	local controldir="$pkgbasedir"/.control.${subpkgname:-$pkgname}
	# the main package depends on -runtime, so declare
	# what runtime provides under the main package as
	# the move to runtime is not a whole directory
	# move like -ocamldoc and -compiler-libs
	if [ "$subpkgname" = "$pkgname-runtime" ]; then
		controldir="$pkgbasedir"/.control.$pkgname
		mkdir -p "$controldir"
	fi

	options_has "!tracedeps" && return 0
	cd "$dir"

	local ocamlobjinfo="$pkgbasedir"/$pkgname/usr/bin/ocamlobjinfo

	ocaml_providers="$(find usr/lib/ocaml -type f \( \
		-name '*.cma' -o -name '*.cmi' -o -name '*.cmo' \
		-o -name '*.cmx' -o -name '*.cmxa' \) 2>/dev/null \
		| /usr/lib/rpm/ocamldeps.sh --provides -f "$ocamlobjinfo" \
		| sed -e 's/^ocaml(/ocaml4-intf:/' \
			-e 's/^ocamlx(/ocaml4-impl:/' \
			-e 's/) = /:/')"

	# Both ocaml-compiler-libs and ocaml-runtime contain a topdirs.cmi
	# So, only allow "ocaml4-intf:Topdirs" in ocaml to avoid conflicts
	local i; for i in $ocaml_providers; do
		if [ "$subpkgname" != "$pkgname-compiler-libs" ] \
			|| [ "${i%:*}" != "ocaml4-intf:Topdirs" ]; then
			echo "$i=$pkgver-r$pkgrel" >> "$controldir"/.provides-py
		fi
	done
}

sha512sums="
64df6462a83058e805ab29d58ef2f3597eb0ed52bf1294a9e6303fb783845cde78c7de04d165e77454a440fa2d50ff10ca81f2d1f9294f2dccdfe28824bce028  ocaml-4.14.2.tar.gz
66a05e85cb00ffa8c6c06f948ea5d3bc6a255296d5bf5ac353c60a4844047876b23b6c1ddea5de1c6849a98ae273f17e3723db49f96da49d5a0bf0d3192ec57c  ocaml4-abuild-find
"
